﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class Services

    Public IServices As New List(Of IService)
    Public Services As ArrayList

    ReadOnly Property Names() As ArrayList
        Get
            Return GetNames()
        End Get
    End Property

    Public Sub New()
        Services = New ArrayList()
        LoadServices()
    End Sub

    Private Sub LoadServices()
        Dim CurrentAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetCallingAssembly
        Dim types As Type() = CurrentAssembly.GetTypes()
        For Each currentType As Type In types
            Dim Interfaces As Type() = currentType.GetInterfaces
            For Each InterfaceType As Type In Interfaces
                If InterfaceType.Equals(GetType(IService)) Then
                    Dim Service As IService = CurrentAssembly.CreateInstance(currentType.FullName)
                    IServices.Add(Service)
                End If
            Next
        Next
    End Sub

    Private Function GetNames() As ArrayList
        Dim arrServices As New ArrayList
        For Each Service As IService In IServices
            arrServices.Add(Service.Name)
        Next
        Return arrServices
    End Function

    Public Shared Function SerializeObject(ByVal Parameter As Object) As String
        Dim formatter1 As BinaryFormatter = New BinaryFormatter()
        Dim stream1 As MemoryStream = New MemoryStream()
        formatter1.Serialize(stream1, Parameter)
        Dim text1 As String = Convert.ToBase64String(stream1.GetBuffer())
        stream1.Close()
        Return text1
    End Function

    Public Shared Function DeserializeObject(ByVal Parameter As String) As Object
        Dim formatter1 As BinaryFormatter = New BinaryFormatter()
        Dim objParam As Object
        Dim str() As Byte = Convert.FromBase64String(Parameter)
        Dim stream1 As MemoryStream = New MemoryStream(str)
        objParam = formatter1.Deserialize(stream1)
        Return objParam
    End Function


End Class
